/* ----------------------------------------------------------------------
 *	Shadow Quest
 *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  ----------------------------------------------------------------------
 */
package sq;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;

import org.lwjgl.LWJGLException;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.xml.sax.SAXException;

/** This class is provided to reduce the complexity of pre-loading resources.
 */
@SuppressWarnings("deprecation")
public class FontLoader {

    private static final int INTRO_FRAMES = 5, WON_FRAMES = 2;
    public static final String DEFAULT_COLOR_STRING = "ff00ff";
    public static final Color DEFAULT_COLOR = Color.magenta;
    private static Font fontOriginal, fontSize9, fontSize10, fontSize11, fontSize12, fontTitle, fontTitle20, fontTitle25;
    private static Color dispNameBg, monsterNameBg, allyNameBg, dialogueBg, rainColor;
    private static Color dangerColor, titleColor;
    private static Color miniPlayer, miniVillager, miniAlly, miniMonster;
    private static ArrayList<String> dialogues;
    private static boolean gameDataLoaded = false;
    

    /** Initialises all common resources (excluding fonts).
     * @throws LWJGLException 
     * @throws IOException 
     */
    public static void initialiseCommonResources()
            throws SlickException {
        loadColors();
        loadFontTitle("assets/res/Livingstone.ttf", 40);
    }

    /** Initialises all other resources (excluding fonts).
     */
    public static void initialiseFonts()
            throws SlickException {
        loadFont("assets/res/DejaVuSans-Bold.ttf", 15);
    }

    /** Initialises all game data.
     */
    public static void initialiseGameData()
            throws SlickException {
        try {
            FileIO.loadGameData("data/data.xml");
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }

        gameDataLoaded = true;

    }

    /** Loads the colors.
     */
    private static void loadColors()
            throws SlickException {

        dispNameBg = new Color(0, 0, 0, 0.8f);
        monsterNameBg = new Color(0.4f, 0, 0, 0.8f);
        allyNameBg = new Color(0, 0.4f, 0, 0.8f);
        dialogueBg = new Color(0f, 0f, 0.4f, 0.6f);
        rainColor = new Color(0.8f, 0.8f, 0.8f, 0.2f);

        titleColor = new Color(0.2f, 0f, 0f, 0.9f);
        ;

        dangerColor = new Color(1f, 0f, 0f, 0.5f);

        miniPlayer = Color.white;
        miniVillager = Color.blue;
        miniAlly = Color.green;
        miniMonster = Color.red;

    }

    /** Loads a font from a TTF file.
     * @param ttf_filename Path to a TTF font file to use to draw text.
     * @param textsize Font size, in points.
     */
    private static void loadFont(String ttf_filename, float textsize)
            throws SlickException {
        // First, load a java.awt.Font of the given TTF file
        java.awt.Font awtfont;

        try {
            awtfont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT,
                    new File(ttf_filename));
        } catch (java.io.IOException e) {
            throw new SlickException("Could not open font file "
                    + ttf_filename);
        } catch (java.awt.FontFormatException e) {
            throw new SlickException("Could not load font " + ttf_filename);
        }
        // Create a "derived font" with the correct font size
        awtfont = awtfont.deriveFont(textsize);
        fontOriginal = new TrueTypeFont(awtfont, true);
        fontSize9 = new TrueTypeFont(awtfont.deriveFont(9f), true);
        fontSize10 = new TrueTypeFont(awtfont.deriveFont(10f), true);
        fontSize11 = new TrueTypeFont(awtfont.deriveFont(11f), true);
        fontSize12 = new TrueTypeFont(awtfont.deriveFont(12f), true);

    }

    /** Loads a font from a TTF file.
     * @param ttf_filename Path to a TTF font file to use to draw text.
     * @param textsize Font size, in points.
     */
    private static void loadFontTitle(String ttf_filename, float textsize)
            throws SlickException {
        // First, load a java.awt.Font of the given TTF file
        java.awt.Font awtfont;

        try {
            awtfont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT,
                    new File(ttf_filename));
        } catch (java.io.IOException e) {
            throw new SlickException("Could not open font file "
                    + ttf_filename);
        } catch (java.awt.FontFormatException e) {
            throw new SlickException("Could not load font " + ttf_filename);
        }

        // Create a "derived font" with the correct font size
        fontTitle = new TrueTypeFont(awtfont.deriveFont(textsize), true);
        fontTitle20 = new TrueTypeFont(awtfont.deriveFont(20f), true);
        fontTitle25 = new TrueTypeFont(awtfont.deriveFont(25f), true);

    }

    /** Gets the default. 
     * @return The font.
     */
    public static Font getFontOriginal() {
        return fontOriginal;
    }

    /** Gets the default set to size 9. 
     * @return The font.
     */
    public static Font getFontSize9() {
        return fontSize9;
    }

    /** Gets the default set to size 10. 
     * @return The font.
     */
    public static Font getFontSize10() {
        return fontSize10;
    }

    /** Gets the default set to size 11. 
     * @return The font.
     */
    public static Font getFontSize11() {
        return fontSize11;
    }

    /** Gets the default set to size 12. 
     * @return The font.
     */
    public static Font getFontSize12() {
        return fontSize12;
    }

    /** Gets the default set to size 12. 
     * @return The font.
     */
    public static Font getFontTitle() {
        return fontTitle;
    }

    /** Gets the default set to size 12. 
     * @return The font.
     */
    public static Font getFontTitle25() {
        return fontTitle25;
    }

    /** Gets the default set to size 12. 
     * @return The font.
     */
    public static Font getFontTitle20() {
        return fontTitle20;
    }

    /** Gets the color that is used for Display name background.
     * @return The background color.
     */
    public static Color getDisplayBgColor() {
        return dispNameBg;
    }

    /** Gets the color that is used for Display name background for Monsters.
     * @return The background color.
     */
    public static Color getMonesterBgColor() {
        return monsterNameBg;
    }

    /** Gets the color that is used for Display name background for Allies.
     * @return The background color.
     */
    public static Color getAllyBgColor() {
        return allyNameBg;
    }

    /** Gets the color that is used for dialogue background.
     * @return The background color.
     */
    public static Color getDialogueBgColor() {
        return dialogueBg;
    }

    /** Gets the rain color.
     * @return The rain color.
     */
    public static Color getRainColor() {
        return rainColor;
    }

    /** Gets the corresponding color of a hex code.
     * @param str Hex-code of the color.
     * @return The image.
     */
    public static Color getColor(String str) {
        if (str.length() == 6) {
            String r = str.substring(0, 2), g = str.substring(2, 4), b = str.substring(4, 6);
            return new Color(Integer.parseInt(r, 16), Integer.parseInt(g, 16), Integer.parseInt(b, 16));
        } else {
            return DEFAULT_COLOR;
        }
    }

    /** Gets the corresponding color of a hex code, given the original color is of the DEFAULT_COLOUR.
     * @param str Hex-code of the color.
     * @return The image.
     */
    public static Color getColor(String str, Color clr) {
        if (str.length() == 6) {
            String r = str.substring(0, 2), g = str.substring(2, 4), b = str.substring(4, 6);
            return new Color(Integer.parseInt(r, 16), Integer.parseInt(g, 16), Integer.parseInt(b, 16));
        } else {
            return clr;
        }
    }

    /** Gets the minimap Player color.
     * @return The minimap Player color.
     */
    public static Color getMinimapPlayerColor() {
        return miniPlayer;
    }

    /** Gets the minimap Villager color.
     * @return The minimap Villager color.
     */
    public static Color getMinimapVillagerColor() {
        return miniVillager;
    }

    /** Gets the minimap Ally color.
     * @return The minimap Ally color.
     */
    public static Color getMinimapAllyColor() {
        return miniAlly;
    }

    /** Gets the minimap Monster color.
     * @return The minimap Monster color.
     */
    public static Color getMinimapMonsterColor() {
        return miniMonster;
    }

    /** Gets the Danger color.
     * @return The Danger color.
     */
    public static Color getDangerColor() {
        return dangerColor;
    }

    /** Checks if the game Data has been loaded already.
     * @return True if the game data has been loaded.
     */
    public static boolean isGameDataLoaded() {
        return gameDataLoaded;
    }

    public static ArrayList<String> getDialogues() {
        return dialogues;
    }

    public static Color getTitleColor() {
        return titleColor;
    }
}
